package love.yuanmengrobot.als.system.filter;

import love.yuanmengrobot.als.system.javaBean.AlsUser;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

public class Filter implements javax.servlet.Filter {
    public void destroy() {
    }

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        System.out.println("过滤器接收");
        HttpServletRequest httpServlet=(HttpServletRequest)req;
       AlsUser alsUser=(AlsUser) httpServlet.getSession().getAttribute("alsUser");
       String s= httpServlet.getRequestURI();
        System.out.println(s);


        if(alsUser!=null){
           System.out.println(alsUser.toString());
            chain.doFilter(req, resp);
       }else {
           System.out.println("未登录！");
            if(s!=null){//请求登录的话，快去吧！
                if("/als/".equals(s)||s.contains("/login")||s.contains("/resources")){//这个是可以优化的，有bug哦！作为后门了^v^
                    System.out.println("允许跳转");
                    chain.doFilter(req, resp);
                    return;
                }
            }else {//去登录吧！
                req.getRequestDispatcher("WEB-INF/view/system/login/login.jsp").forward(req, resp);
            }

       }


    }

    public void init(FilterConfig config) throws ServletException {

    }

}
